package com.labo.erpcrm.dao;

import java.util.List;


import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import com.labo.erpcrm.bean.Client;

public class ClientDao extends GenericRepository<Client, Integer> {



	public Client GetByMail(String email){
		Session s = HelperSessionFactory.getSession();
		Transaction trans = s.beginTransaction();
		Criteria criteria = s.createCriteria(Client.class).add(Restrictions.eq("email_client", email));
		Client client = (Client) criteria.uniqueResult();
		trans.commit();
		return client;
	}

	public Client GetById(Integer Id){
		Session s = HelperSessionFactory.getSession();
		Transaction trans = s.beginTransaction();
		Criteria criteria = s.createCriteria(Client.class).add(Restrictions.eq("id_client", Id));
		Client client = (Client) criteria.uniqueResult();
		trans.commit();
		return client;
	}

	public List<Client> GetAllByName(String name){
		Session s = HelperSessionFactory.getSession();
		Transaction trans = s.beginTransaction();
		Criteria criteria = s.createCriteria(Client.class).add(Restrictions.like("nom_client", name+"%"));
		@SuppressWarnings("unchecked")
		List<Client> lstClient = (List<Client>) criteria.list();
		trans.commit();
		return lstClient;
	}

	@SuppressWarnings("unchecked")
	public List<Client> GetAll(){
		Session s = HelperSessionFactory.getSession();
		Transaction trans = s.beginTransaction();
		Criteria criteria = s.createCriteria(Client.class);
		List<Client> lstClient = (List<Client>) criteria.list();
		trans.commit();
		return lstClient ;
	}

	@Override
	public void Save(Client obj) {
		// TODO Auto-generated method stub
		super.Save(obj);
	}

	@Override
	public void SaveOrUpdate(Client obj) {
		// TODO Auto-generated method stubs
		System.out.println("ID CLIENT : "+obj.getId_client());
		super.SaveOrUpdate(obj);
	}

	@Override
	public void Update(Client obj) {
		// TODO Auto-generated method stub
		super.Update(obj);
	}

	@Override
	public void Delete(Client obj) {
		// TODO Auto-generated method stub
		super.Delete(obj);
	}

}
